//引入express模块
const express = require('express');
//导入连接池对象
const pool = require('../pool.js');
//创建路由
const router = express.Router(); 
//登录模块
router.post('/v1/login',(req,res)=>{
    let obj = req.body;
    console.log(obj.uname,obj.upwd)
    let sql = "select * from dd_user where uname=? and upwd=md5(?)";
    pool.query(sql,[obj.uname,obj.upwd],(err,result)=>{
        if(err) throw err;
        console.log(result);
        if(result.length>0){
            res.send('1')
        }else{
            res.send('0')
        };
    });
    // res.send("登陆成功")
});
//注册模块
router.post('/v1/reg',(req,res)=>{
    var obj = req.body;
    var sql = "INSERT INTO dd_user(id,uname,upwd) VALUES(null,?,md5(?));"
    pool.query(sql,[obj.uname,obj.upwd],(err,result)=>{
        if(err) throw err;
        if(result.affectedRows>0){
            res.send('1')
        }else{
            res.send('0')
        }
    });
});

//检查用户名是否存在
router.get('/v1/exists',(req,res)=>{
    console.log(req.query.uname)
    var obj = req.query;
    var sql = "SELECT id FROM dd_user WHERE uname=?";
    //console.log(uname)
    pool.query(sql,[obj.uname],(err,result)=>{
        if(err) throw err;
        if(result.length>0){
            res.send('1')
        }else{
            res.send('0')
        }
    })
})
//导出路由
module.exports = router;